package com.tabjin.rfidsocket.service.sys.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.tabjin.rfidsocket.common.RequestHolder;
import com.tabjin.rfidsocket.exception.ParamException;
import com.tabjin.rfidsocket.mapper.mapper.SysRoleAclCustomMapper;
import com.tabjin.rfidsocket.mapper.mapper.SysRoleAclMapper;
import com.tabjin.rfidsocket.pojo.SysRoleAcl;
import com.tabjin.rfidsocket.service.sys.SysRoleAclService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author tabjin
 * create at 2020/12/31 12:03
 * @program rfid_socket
 * @description
 */
@Service
public class SysRoleAclServiceImpl implements SysRoleAclService {

    private final SysRoleAclCustomMapper sysRoleAclCustomMapper;
    private final SysRoleAclMapper sysRoleAclMapper;

    public SysRoleAclServiceImpl(SysRoleAclCustomMapper sysRoleAclCustomMapper, SysRoleAclMapper sysRoleAclMapper) {
        this.sysRoleAclCustomMapper = sysRoleAclCustomMapper;
        this.sysRoleAclMapper = sysRoleAclMapper;
    }

    /**
     * 更改角色权限
     *
     * @param roleId
     * @param aclIdList
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void changeRoleAcls(Integer roleId, List<Integer> aclIdList) {
        List<Integer> originAclIdList = sysRoleAclCustomMapper.getAclIdListByRoleIdList(Lists.newArrayList(roleId));
        if (originAclIdList.size() == aclIdList.size()) {
            Set<Integer> originAclIdSet = Sets.newHashSet(originAclIdList);
            Set<Integer> aclIdSet = Sets.newHashSet(aclIdList);
            originAclIdSet.removeAll(aclIdList);
            if (CollectionUtils.isEmpty(aclIdSet)) {
                return;
            }
        }
        updateRoleAcls(roleId, aclIdList);
    }

    /**
     * 更新
     *
     * @param roleId
     * @param aclIdList
     */
    public void updateRoleAcls(Integer roleId, List<Integer> aclIdList) {
        sysRoleAclCustomMapper.deleteByRoleId(roleId);// 直接删除之后更新
        if (CollectionUtils.isEmpty(aclIdList)) {
            return;
        }
        List<SysRoleAcl> roleAclList = Lists.newArrayList();
        for (Integer aclId : aclIdList) {
            SysRoleAcl sysRoleAcl = SysRoleAcl.builder()
                    .roleId(roleId)
                    .aclId(aclId)
                    .operator(RequestHolder.getCurrentUser().getUsername())
                    .operateIp("127.0.0.1")
                    .operateTime(new Date())
                    .build();
            roleAclList.add(sysRoleAcl);
        }
        sysRoleAclCustomMapper.batchInsert(roleAclList);
    }


    /**
     * 根据角色获取权限
     *
     * @param roleId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Integer> getAclsByRoleId(Integer roleId) {
        if (roleId == null) {
            throw new ParamException("无角色ID");
        }

        Example example = new Example(SysRoleAcl.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", roleId);
        List<SysRoleAcl> sysRoleAclList = sysRoleAclMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(sysRoleAclList)) {
            return Lists.newArrayList();
        }
        return sysRoleAclList.stream().map(item -> item.getAclId()).collect(Collectors.toList());
    }
}
